package org.gpp.proj1.ui.gui.profile;

import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;

import org.gpp.proj1.ui.gui.SRGGUIView;
import org.gpp.proj1.ui.gui.command.SRGGUICommandFactory;

/**
 * Created with a little help from netbeans 
 * 
 * @author Paolo Victor, paolovictor@gmail.com
 */
public class ProfileSelectionFrame extends JFrame {
	private static final long serialVersionUID = 7817242292202614005L;
	
	private JButton createProfileButton;
	private JButton okButton;
	private JButton cancelButton;
	private JLabel jLabel1;
	private JList profileList;
	private JScrollPane profileScrollPane;

	private SRGGUIView parentUI;
	
	public ProfileSelectionFrame( SRGGUIView parent ) {
		this.parentUI = parent;
		
		initComponents();
		
		this.setLocationRelativeTo( null );
	}

	private void initComponents() {
		jLabel1 = new javax.swing.JLabel();
		profileScrollPane = new javax.swing.JScrollPane();
		profileList = new javax.swing.JList();
		createProfileButton = new javax.swing.JButton();
		okButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE );
		setTitle("Profile selection");
		setResizable(false);
		jLabel1.setText("Select a player profile");

		profileList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		profileList.setToolTipText("Select the player profile that will be used through the game");
		profileScrollPane.setViewportView(profileList);
		
		this.refreshProfiles();

		createProfileButton.setText("Create Profile...");
		createProfileButton.setToolTipText("Create a new player profile");
		createProfileButton.setActionCommand( SRGGUICommandFactory.CREATE_PROFILE );
		createProfileButton.addActionListener( parentUI );

		okButton.setText("OK");
		okButton.setToolTipText("Load the profile and start the game");
		okButton.setActionCommand( SRGGUICommandFactory.LOAD_PROFILE );
		okButton.addActionListener( parentUI );

		cancelButton.setText("Cancel");
		cancelButton.setToolTipText("Cancel the selection and quit the game");
		cancelButton.setActionCommand( SRGGUICommandFactory.CANCEL_PROFILE_LOADING );
		cancelButton.addActionListener( parentUI );
		
		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
								.add(jLabel1)
								.add(layout.createSequentialGroup()
										.add(createProfileButton)
										.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
										.add(cancelButton)
										.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.add(okButton))
										.add(profileScrollPane))
										.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(jLabel1)
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
						.add(profileScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 277, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
								.add(createProfileButton)
								.add(okButton)
								.add(cancelButton))
								.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		pack();
	}

	public Object getSelectedProfile() {
		int selectedIndex = profileList.getSelectedIndex();
		
		if( selectedIndex != -1 ) {
			return profileList.getModel().getElementAt( selectedIndex );
		}
		
		return null;
	}

	public void refreshProfiles() {
		Set<String> nameSet = parentUI.getController().getPlayerProfileNames();
		final String[] nameArray = new String[nameSet.size()];
		int k = 0;
		for( String name : nameSet ) {
			nameArray[k] = name;
			k++;
		}
		
		profileList.setModel(new javax.swing.AbstractListModel() {
			private static final long serialVersionUID = -9098539865502359884L;
			
			String[] strings = nameArray;
			public int getSize() { return strings.length; }
			public Object getElementAt(int i) { return strings[i]; }
		});		
	}
}